/**
 * Copyright 2008-2009 HAN.BIFANG
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 */
package han.bifang.business.dao;

import han.bifang.common.entity.SoaMappingService;
import han.bifang.common.exception.BifangErrorCode;
import han.bifang.common.exception.BifangException;
import han.bifang.common.persist.dao.DaoManager;
import han.bifang.common.persist.page.Page;
import han.bifang.common.persist.page.PagedQuery;
import han.bifang.common.persist.tx.Tx;
import han.bifang.common.persist.tx.TxManager;
import han.bifang.common.utils.CommonUtils;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * @Author Kent
 */
public class MappingServiceDao {
	public static void createService(SoaMappingService s)
			throws BifangException {
		Tx tx = null;

		try {
			tx = TxManager.start();
			String hql = "from SoaService s where s.serviceCode = ? ";

			List ses = DaoManager.getDao().find(hql, s.getLocalCode());
			if (ses.size() > 0) {
				throw new BifangException(
						BifangErrorCode.SERVICE_CODE_EXISTS_ERROR,
						"Serivce code " + s.getLocalCode() + " exists.");

			}

			hql = "from SoaMappingService s where s.localCode = ? ";

			ses = DaoManager.getDao().find(hql, s.getLocalCode());
			if (ses.size() > 0) {
				throw new BifangException(
						BifangErrorCode.SERVICE_CODE_EXISTS_ERROR,
						"Serivce code " + s.getLocalCode() + " exists.");
			}
			DaoManager.getDao().save(s);
			DaoManager.getDao().flush();
			tx.commit();

		} catch (BifangException e) {
			throw e;
		} catch (Throwable e) {
			Log logger = LogFactory.getLog(MappingServiceDao.class);
			logger.error("MappingServiceDao.createService()", e);
			tx.rollback();
			throw new BifangException(BifangErrorCode.DB_ERROR,
					"createService() error", e);
		} finally {
			tx.end();
		}

	}

	public static void modifyService(SoaMappingService s)
			throws BifangException {
		Tx tx = null;

		try {
			tx = TxManager.start();
			String hql = "from SoaService s where s.serviceCode = ? ";

			List ses = DaoManager.getDao().find(hql, s.getLocalCode());
			if (ses.size() > 0) {
				throw new BifangException(
						BifangErrorCode.SERVICE_CODE_EXISTS_ERROR,
						"Serivce code " + s.getLocalCode() + " exists.");

			}

			hql = "from SoaMappingService s where s.localCode = ? ";

			ses = DaoManager.getDao().find(hql, s.getLocalCode());
			if (ses.size() > 0) {
				SoaMappingService t = (SoaMappingService) ses.get(0);
				if (!t.getId().equals(s.getId()))
					throw new BifangException(
							BifangErrorCode.SERVICE_CODE_EXISTS_ERROR,
							"Serivce code " + s.getLocalCode() + " exists.");
			}
			DaoManager.getDao().merge(s);
			DaoManager.getDao().flush();
			tx.commit();

		} catch (BifangException e) {
			throw e;
		} catch (Throwable e) {
			Log logger = LogFactory.getLog(MappingServiceDao.class);
			logger.error("MappingServiceDao.modifyService()", e);
			tx.rollback();
			throw new BifangException(BifangErrorCode.DB_ERROR,
					"modifyService() error", e);
		} finally {
			tx.end();
		}

	}

	public static void removeService(String id) throws BifangException {
		Tx tx = null;

		try {
			tx = TxManager.start();
			SoaMappingService s = (SoaMappingService) DaoManager.getDao().get(
					SoaMappingService.class, id);
			DaoManager.getDao().delete(s);
			DaoManager.getDao().flush();
			tx.commit();

		} catch (Throwable e) {
			Log logger = LogFactory.getLog(MappingServiceDao.class);
			logger.error("MappingServiceDao.removeService()", e);
			tx.rollback();
			throw new BifangException(BifangErrorCode.DB_ERROR,
					"removeService() error", e);
		} finally {
			tx.end();
		}

	}

	public static SoaMappingService getMappingService(String id) throws BifangException {
		Tx tx = null;

		try {
			tx = TxManager.start();
			SoaMappingService s = (SoaMappingService) DaoManager.getDao().get(
					SoaMappingService.class, id);
			
			tx.commit();
			return s;
		} catch (Throwable e) {
			Log logger = LogFactory.getLog(MappingServiceDao.class);
			logger.error("MappingServiceDao.getMappingService()", e);
			tx.rollback();
			throw new BifangException(BifangErrorCode.DB_ERROR,
					"getMappingService() error", e);
		} finally {
			tx.end();
		}

	}
	
	public static ArrayList searchService( String hql,int page,int pageSize) throws BifangException {
		Tx tx = null;

		try {
			tx = TxManager.start();
			if(CommonUtils.isEmpty(hql)){
				hql = "from SoaMappingService";
			}
			PagedQuery pq = new PagedQuery(hql,page,pageSize);
			Page p = pq.getPage();
			int total = p.getPaginator().getPageNumber();
			ArrayList ret = new ArrayList();
			ret.add(total);
			ret.add(p.getList());
			tx.commit();
			return ret;
		}  catch (Throwable e) {
			Log logger = LogFactory.getLog(ServiceDao.class);
			logger.error("MappingServiceDao.searchService()", e);
			tx.rollback();
			throw new BifangException(BifangErrorCode.DB_ERROR,
					"searchService() error", e);
		} finally {
			tx.end();
		}

	}

}
